package client.ui.view.panel.good;

import client.assets.Icons;
import client.control.Controller;
import client.ui.component.card.PictureLabel;
import client.ui.component.divider.Divider;
import client.ui.component.scroll.SmallMouseWheelListener;
import client.ui.theme.ThisFont;
import client.ui.util.MaterialBorder;
import client.ui.util.MaterialColors;
import client.ui.view.base.PanelControl;
import client.util.ImageUtil;
import client.entity.Good;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
  * @Description 图片显示框架
  * @author 孔明浩
  * @Date 2020/3/31
  */
public class GoodImagePanel extends JPanel implements PanelControl {
    private List<ImageIcon> images = new ArrayList<>();
    private JPanel thumbPanel;
    private Good good;
    private JLabel imgLabel;
    public GoodImagePanel(Good good){
        super();
        this.good = good;
        initial();
        getImages();
    }

    protected void initial(){
        this.setBackground(MaterialColors.WHITE);
        JLabel label = new JLabel(ImageUtil.getImageIcon(Icons.place_hold,null,45,45));
        BorderLayout borderLayout = new BorderLayout();
        borderLayout.setHgap(0);
        borderLayout.setVgap(0);
        this.setLayout(borderLayout);
        this.setBorder(new LineBorder(MaterialColors.COSMO_BLUE, 2));

        imgLabel = new PictureLabel(Icons.place_hold);

        thumbPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        thumbPanel.setBackground(MaterialColors.WHITE);
        thumbPanel.setBorder(new LineBorder(MaterialColors.COSMO_BLUE));
        JScrollPane scrollPane = new JScrollPane(thumbPanel);
        thumbPanel.add(label);

        scrollPane.setBorder(null);
        scrollPane.setBackground(MaterialColors.WHITE);
        JScrollBar hScrollBar = scrollPane.getHorizontalScrollBar();
        scrollPane.addMouseWheelListener(new SmallMouseWheelListener(10,hScrollBar));
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

        JLabel descriptionPanel = new JLabel("图片展示");
        descriptionPanel.setFont(ThisFont.MIDDLE_FONT);
        descriptionPanel.setBorder(new EmptyBorder(8,8,8,8));

        //加一条分割线
        this.add(descriptionPanel, BorderLayout.NORTH);
        this.add(imgLabel, BorderLayout.CENTER);
        this.add(scrollPane, BorderLayout.SOUTH);
    }

    /**
     * @Description 根据传入good随想跟新图片面板
     * @param object 传入Good对象
     * @author 孔明浩
     * @Date 2020/4/6
     */
    @Override
    public void updatePanel(Object object) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                thumbPanel.removeAll();
                if (images.size() > 0){
                    ImageIcon imageIcon = images.get(0);
                    imgLabel.setBorder(new EmptyBorder(0,25,0,25));
                    imgLabel.setIcon(
                            imageIcon.getIconWidth() > imageIcon.getIconHeight() ?
                                    ImageUtil.getImageIcon(imageIcon,null,130,0):
                                    ImageUtil.getImageIcon(imageIcon,null,0,130)
                    );
                }
                for (ImageIcon imageIcon: images){
                    JLabel label = new JLabel(ImageUtil.getImageIcon(imageIcon,null,45,45));
                    label.setBorder(new EmptyBorder(1,1,1,1));
                    label.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            imgLabel.setBorder(new EmptyBorder(0,25,0,25));
                            imgLabel.setIcon(
                                    imageIcon.getIconWidth() > imageIcon.getIconHeight() ?
                                            ImageUtil.getImageIcon(imageIcon,null,130,0):
                                            ImageUtil.getImageIcon(imageIcon,null,0,130)
                            );
                        }
                        @Override
                        public void mouseEntered(MouseEvent e) {
                            label.setBorder(BorderFactory.createLineBorder(MaterialColors.COSMO_RED));
                        }

                        @Override
                        public void mouseExited(MouseEvent e) {
                            label.setBorder(new EmptyBorder(1, 1, 1, 1));
                        }
                    });
                    thumbPanel.add(label);
                }
                thumbPanel.validate();
                thumbPanel.repaint();
            }
        });
    }

    private void getImages(){
        Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
        singleThreadExecutor.execute(new Runnable() {
            @Override
            public void run() {
                images = Controller.GOOD_CONTROL.getGoodImage(good.getGoodId());
                updatePanel("");
            }
        });
    }
}
